<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>使用lua实现try-catch异常捕获</title>
    <meta name="description" content="lua原生并没有提供try-catch的语法来捕获异常处理，但是提供了pcall/xpcall等接口，可在保护模式下执行lua函数。因此，可以通过封装这两个接口，来实现try-catch块的捕获机制。我们可以先来看下，封装后的try-catch使用方式：try{    -- try 代码块    function...">

    
    <meta name="keywords" content="xmake,lua,try-catch,异常捕获,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2016/12/14/try-catch/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>使用lua实现try-catch异常捕获</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2016-12-14
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#xmake" title="Category: xmake" rel="category">xmake</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a-->
        <a href="/cn/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a>&nbsp;
    
        <!--a href="/tag/#lua" title="Tag: lua" rel="tag">lua</a-->
        <a href="/cn/tag/#lua" title="Tag: lua" rel="tag">lua</a>&nbsp;
    
        <!--a href="/tag/#try-catch" title="Tag: try-catch" rel="tag">try-catch</a-->
        <a href="/cn/tag/#try-catch" title="Tag: try-catch" rel="tag">try-catch</a>&nbsp;
    
        <!--a href="/tag/#%E5%BC%82%E5%B8%B8%E6%8D%95%E8%8E%B7" title="Tag: 异常捕获" rel="tag">异常捕获</a-->
        <a href="/cn/tag/#异常捕获" title="Tag: 异常捕获" rel="tag">异常捕获</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p>lua原生并没有提供try-catch的语法来捕获异常处理，但是提供了<code class="highlighter-rouge">pcall/xpcall</code>等接口，可在保护模式下执行lua函数。</p>

<p>因此，可以通过封装这两个接口，来实现try-catch块的捕获机制。</p>

<p>我们可以先来看下，封装后的try-catch使用方式：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">try</span>
<span class="p">{</span>
    <span class="c1">-- try 代码块</span>
    <span class="k">function</span> <span class="p">()</span>
        <span class="nb">error</span><span class="p">(</span><span class="s2">"error message"</span><span class="p">)</span>
    <span class="k">end</span><span class="p">,</span>

    <span class="c1">-- catch 代码块</span>
    <span class="n">catch</span> 
    <span class="p">{</span>
        <span class="c1">-- 发生异常后，被执行</span>
        <span class="k">function</span> <span class="p">(</span><span class="n">errors</span><span class="p">)</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>
        <span class="k">end</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<p>上面的代码中，在try块内部认为引发了一个异常，并且抛出错误消息，在catch中进行了捕获，并且将错误消息进行输出显示。</p>

<p>这里除了对<code class="highlighter-rouge">pcall/xpcall</code>进行了封装，用来捕获异常信息，还利用了lua的函数调用语法特性，在只有一个参数传递的情况下，lua可以直接传递一个table类型，并且省略<code class="highlighter-rouge">()</code></p>

<p>其实try后面的整个<code class="highlighter-rouge"><span class="p">{</span><span class="err">...</span><span class="p">}</span></code> 都是一个table而已，作为参数传递给了try函数，其具体实现如下：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">function</span> <span class="nf">try</span><span class="p">(</span><span class="n">block</span><span class="p">)</span>

    <span class="c1">-- get the try function</span>
    <span class="kd">local</span> <span class="n">try</span> <span class="o">=</span> <span class="n">block</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="nb">assert</span><span class="p">(</span><span class="n">try</span><span class="p">)</span>

    <span class="c1">-- get catch and finally functions</span>
    <span class="kd">local</span> <span class="n">funcs</span> <span class="o">=</span> <span class="n">block</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">funcs</span> <span class="ow">and</span> <span class="n">block</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="k">then</span>
        <span class="n">table</span><span class="p">.</span><span class="n">join2</span><span class="p">(</span><span class="n">funcs</span><span class="p">,</span> <span class="n">block</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
    <span class="k">end</span>

    <span class="c1">-- try to call it</span>
    <span class="kd">local</span> <span class="n">ok</span><span class="p">,</span> <span class="n">errors</span> <span class="o">=</span> <span class="nb">pcall</span><span class="p">(</span><span class="n">try</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">ok</span> <span class="k">then</span>

        <span class="c1">-- run the catch function</span>
        <span class="k">if</span> <span class="n">funcs</span> <span class="ow">and</span> <span class="n">funcs</span><span class="p">.</span><span class="n">catch</span> <span class="k">then</span>
            <span class="n">funcs</span><span class="p">.</span><span class="n">catch</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>
        <span class="k">end</span>
    <span class="k">end</span>

    <span class="c1">-- run the finally function</span>
    <span class="k">if</span> <span class="n">funcs</span> <span class="ow">and</span> <span class="n">funcs</span><span class="p">.</span><span class="n">finally</span> <span class="k">then</span>
        <span class="n">funcs</span><span class="p">.</span><span class="n">finally</span><span class="p">(</span><span class="n">ok</span><span class="p">,</span> <span class="n">errors</span><span class="p">)</span>
    <span class="k">end</span>

    <span class="c1">-- ok?</span>
    <span class="k">if</span> <span class="n">ok</span> <span class="k">then</span>
        <span class="k">return</span> <span class="n">errors</span>
    <span class="k">end</span>
<span class="k">end</span>
</code></pre>
</div>

<p>可以看到这里用了<code class="highlighter-rouge">pcall</code>来实际调用try块里面的函数，这样就算函数内部出现异常，也不会中断程序，<code class="highlighter-rouge">pcall</code>会返回false表示运行失败</p>

<p>并且返回实际的出错信息，如果想要自定义格式化错误信息，可以通过调用xpcall来替换pcall，这个接口与pcall相比，多了个错误处理函数：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="kd">local</span> <span class="n">ok</span><span class="p">,</span> <span class="n">errors</span> <span class="o">=</span> <span class="nb">xpcall</span><span class="p">(</span><span class="n">try</span><span class="p">,</span> <span class="nb">debug.traceback</span><span class="p">)</span>
</code></pre>
</div>

<p>你可以直接传入<code class="highlighter-rouge">debug.traceback</code>，使用默认的traceback处理接口，也可以自定义一个处理函数：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="c1">-- traceback</span>
<span class="k">function</span> <span class="nf">my_traceback</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>

    <span class="c1">-- make results</span>
    <span class="kd">local</span> <span class="n">level</span> <span class="o">=</span> <span class="mi">2</span>    
    <span class="k">while</span> <span class="kc">true</span> <span class="k">do</span>    

        <span class="c1">-- get debug info</span>
        <span class="kd">local</span> <span class="n">info</span> <span class="o">=</span> <span class="nb">debug.getinfo</span><span class="p">(</span><span class="n">level</span><span class="p">,</span> <span class="s2">"Sln"</span><span class="p">)</span>

        <span class="c1">-- end?</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">info</span> <span class="ow">or</span> <span class="p">(</span><span class="n">info</span><span class="p">.</span><span class="n">name</span> <span class="ow">and</span> <span class="n">info</span><span class="p">.</span><span class="n">name</span> <span class="o">==</span> <span class="s2">"xpcall"</span><span class="p">)</span> <span class="k">then</span>
            <span class="k">break</span>
        <span class="k">end</span>

        <span class="c1">-- function?</span>
        <span class="k">if</span> <span class="n">info</span><span class="p">.</span><span class="n">what</span> <span class="o">==</span> <span class="s2">"C"</span> <span class="k">then</span>
            <span class="n">results</span> <span class="o">=</span> <span class="n">results</span> <span class="o">..</span> <span class="nb">string.format</span><span class="p">(</span><span class="s2">"    [C]: in function '%s'\n"</span><span class="p">,</span> <span class="n">info</span><span class="p">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">elseif</span> <span class="n">info</span><span class="p">.</span><span class="n">name</span> <span class="k">then</span> 
            <span class="n">results</span> <span class="o">=</span> <span class="n">results</span> <span class="o">..</span> <span class="nb">string.format</span><span class="p">(</span><span class="s2">"    [%s:%d]: in function '%s'\n"</span><span class="p">,</span> <span class="n">info</span><span class="p">.</span><span class="n">short_src</span><span class="p">,</span> <span class="n">info</span><span class="p">.</span><span class="n">currentline</span><span class="p">,</span> <span class="n">info</span><span class="p">.</span><span class="n">name</span><span class="p">)</span>    
        <span class="k">elseif</span> <span class="n">info</span><span class="p">.</span><span class="n">what</span> <span class="o">==</span> <span class="s2">"main"</span> <span class="k">then</span>
            <span class="n">results</span> <span class="o">=</span> <span class="n">results</span> <span class="o">..</span> <span class="nb">string.format</span><span class="p">(</span><span class="s2">"    [%s:%d]: in main chunk\n"</span><span class="p">,</span> <span class="n">info</span><span class="p">.</span><span class="n">short_src</span><span class="p">,</span> <span class="n">info</span><span class="p">.</span><span class="n">currentline</span><span class="p">)</span>    
            <span class="k">break</span>
        <span class="k">else</span>
            <span class="n">results</span> <span class="o">=</span> <span class="n">results</span> <span class="o">..</span> <span class="nb">string.format</span><span class="p">(</span><span class="s2">"    [%s:%d]:\n"</span><span class="p">,</span> <span class="n">info</span><span class="p">.</span><span class="n">short_src</span><span class="p">,</span> <span class="n">info</span><span class="p">.</span><span class="n">currentline</span><span class="p">)</span>    
        <span class="k">end</span>

        <span class="c1">-- next</span>
        <span class="n">level</span> <span class="o">=</span> <span class="n">level</span> <span class="o">+</span> <span class="mi">1</span>    
    <span class="k">end</span>    

    <span class="c1">-- ok?</span>
    <span class="k">return</span> <span class="n">results</span>
<span class="k">end</span>

<span class="c1">-- 调用自定义traceback函数</span>
<span class="kd">local</span> <span class="n">ok</span><span class="p">,</span> <span class="n">errors</span> <span class="o">=</span> <span class="nb">xpcall</span><span class="p">(</span><span class="n">try</span><span class="p">,</span> <span class="n">my_traceback</span><span class="p">)</span>
</code></pre>
</div>

<p>回到try-catch上来，通过上面的实现，会发现里面其实还有个finally的处理，这个的作用是对于<code class="highlighter-rouge">try{}</code>代码块，不管是否执行成功，都会执行到finally块中</p>

<p>也就说，其实上面的实现，完整的支持语法是：<code class="highlighter-rouge">try-catch-finally</code>模式，其中catch和finally都是可选的，根据自己的实际需求提供</p>

<p>例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">try</span>
<span class="p">{</span>
    <span class="c1">-- try 代码块</span>
    <span class="k">function</span> <span class="p">()</span>
        <span class="nb">error</span><span class="p">(</span><span class="s2">"error message"</span><span class="p">)</span>
    <span class="k">end</span><span class="p">,</span>

    <span class="c1">-- catch 代码块</span>
    <span class="n">catch</span> 
    <span class="p">{</span>
        <span class="c1">-- 发生异常后，被执行</span>
        <span class="k">function</span> <span class="p">(</span><span class="n">errors</span><span class="p">)</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>
        <span class="k">end</span>
    <span class="p">},</span>

    <span class="c1">-- finally 代码块</span>
    <span class="n">finally</span> 
    <span class="p">{</span>
        <span class="c1">-- 最后都会执行到这里</span>
        <span class="k">function</span> <span class="p">(</span><span class="n">ok</span><span class="p">,</span> <span class="n">errors</span><span class="p">)</span>
            <span class="c1">-- 如果try{}中存在异常，ok为true，errors为错误信息，否则为false，errors为try中的返回值</span>
        <span class="k">end</span>
    <span class="p">}</span>
<span class="p">}</span>

</code></pre>
</div>

<p>或者只有finally块：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">try</span>
<span class="p">{</span>
    <span class="c1">-- try 代码块</span>
    <span class="k">function</span> <span class="p">()</span>
        <span class="k">return</span> <span class="s2">"info"</span>
    <span class="k">end</span><span class="p">,</span>

    <span class="c1">-- finally 代码块</span>
    <span class="n">finally</span> 
    <span class="p">{</span>
        <span class="c1">-- 由于此try代码没发生异常，因此ok为true，errors为返回值: "info"</span>
        <span class="k">function</span> <span class="p">(</span><span class="n">ok</span><span class="p">,</span> <span class="n">errors</span><span class="p">)</span>
        <span class="k">end</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<p>处理可以在finally中获取try里面的正常返回值，其实在仅有try的情况下，也是可以获取返回值的：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="c1">-- 如果没发生异常，result 为返回值："xxxx"，否则为nil</span>
<span class="kd">local</span> <span class="n">result</span> <span class="o">=</span> <span class="n">try</span>
<span class="p">{</span>
    <span class="k">function</span> <span class="p">()</span>
        <span class="k">return</span> <span class="s2">"xxxx"</span>
    <span class="k">end</span>
<span class="p">}</span>
</code></pre>
</div>

<p>可以看到，这个基于pcall的<code class="highlighter-rouge">try-catch-finally</code>异常捕获封装，使用上还是非常灵活的，而且其实现相当的简单</p>

<p>这也充分说明了lua是一门已非常强大灵活，又非常精简的语言。</p>

<p>在<a href="http://www.xmake.io/cn">xmake</a>的自定义脚本、插件开发中，也是完全基于此异常捕获机制</p>

<p>这样使得扩展脚本的开发非常的精简可读，省去了繁琐的<code class="highlighter-rouge">if err ~= nil then</code>返回值判断，在发生错误时，xmake会直接抛出异常进行中断，然后高亮提示详细的错误信息。</p>

<p>例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>

    <span class="c1">-- 在编译完ios程序后，对目标程序进行ldid签名</span>
    <span class="n">after_build</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">))</span>
        <span class="n">os</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="s2">"ldid -S %s"</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span><span class="n">targetfile</span><span class="p">())</span>
    <span class="k">end</span>
</code></pre>
</div>

<p>只需要一行<code class="highlighter-rouge">os.run</code>就行了，也不需要返回值判断是否运行成功，因为运行失败后，xmake会自动抛异常，中断程序并且提示错误</p>

<p>如果你想在运行失败后，不直接中断xmake，继续往下运行，可以自己加个try快就行了：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>

    <span class="n">after_build</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">))</span>
        <span class="n">try</span>
        <span class="p">{</span>
            <span class="k">function</span> <span class="p">()</span>
                <span class="n">os</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="s2">"ldid -S %s"</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span><span class="n">targetfile</span><span class="p">())</span>
            <span class="k">end</span>
        <span class="p">}</span>
    <span class="k">end</span>
</code></pre>
</div>

<p>如果还想捕获出错信息，可以再加个catch:</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>

    <span class="n">after_build</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">))</span>
        <span class="n">try</span>
        <span class="p">{</span>
            <span class="k">function</span> <span class="p">()</span>
                <span class="n">os</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="s2">"ldid -S %s"</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span><span class="n">targetfile</span><span class="p">())</span>
            <span class="k">end</span><span class="p">,</span>
            <span class="n">catch</span> 
            <span class="p">{</span>
                <span class="k">function</span> <span class="p">(</span><span class="n">errors</span><span class="p">)</span>
                    <span class="nb">print</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>
                <span class="k">end</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="k">end</span>
</code></pre>
</div>

<p>不过一般情况下，在xmake中写自定义脚本，是不需要手动加try-catch的，直接调用各种api，出错后让xmake默认的处理程序接管，直接中断就行了。。</p>

<p>最后附上<code class="highlighter-rouge">try-catch-finally</code>实现的相关完整源码：</p>

<ul>
  <li><a href="https://github.com/waruqi/xmake/blob/master/xmake/core/sandbox/modules/try.lua">try</a></li>
  <li><a href="https://github.com/waruqi/xmake/blob/master/xmake/core/sandbox/modules/catch.lua">catch</a></li>
  <li><a href="https://github.com/waruqi/xmake/blob/master/xmake/core/sandbox/modules/finally.lua">finally</a></li>
</ul>

        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        

        
        
            
        
            
            
            
                
                    
                        
                        <h2 id="similar_posts">相关文章</h2>
                        <ul>
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/17/xmake-update-v2.6.2/">xmake v2.6.2 发布，新增 Linux 内核驱动模块构建支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/03/xmake-update-v2.6.1/">xmake v2.6.1 发布，使用 Lua5.4 运行时，Rust 和 C++ 混合编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/30/xmake-update-v2.5.9/">xmake v2.5.9 发布，改进 C++20 模块，并支持 Nim, Keil MDK 和 Unity Build
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/08/xmake-update-v2.5.8/">xmake v2.5.8 发布，新增 Pascal/Swig 程序和 Lua53 运行时支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/08/29/xmake-update-v2.5.7/">xmake v2.5.7 发布，包依赖锁定和 Vala/Metal 语言编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
        
            </ul>
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2016/12/07/update-v1.6.1/">tbox v1.6.1更新，新增stackless协程支持</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2017/01/03/bearychat/">TBOOX新建bearychat团队交流群</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
